package com.atguigu.orderservice.service.impl;

import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.orderservice.client.EduClient;
import com.atguigu.orderservice.client.UcenterClient;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.mapper.OrderMapper;
import com.atguigu.orderservice.service.OrderService;

import com.atguigu.orderservice.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-02-07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    //注入两个远程模块（UcenterClient和EduClient）
    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private EduClient eduClient;


    //1、生成订单方法
    @Override
    public String createOrders(String courseId, String memberId) {
        //1、通过远程调用根据 用户id 获取 用户信息
        UcenterMemberOrder ucenterMemberOrder = ucenterClient.getInfotwo(memberId);

        //2、通过远程调用根据 课程id 获取 课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //3、创建order对象，向order对象里面设置数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号（使用工具类生成）
        order.setCourseId(courseId);//课程id
        order.setCourseTitle(courseInfoOrder.getTitle());
        order.setCourseCover(courseInfoOrder.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterMemberOrder.getMobile());
        order.setNickname(ucenterMemberOrder.getNickname());
        order.setStatus(0);//订单状态（0：未支付 1：已支付）
        order.setPayType(1);//支付类型，微信1

        //4、加入到数据库
        baseMapper.insert(order);

        //5、返回订单号
        return order.getOrderNo();
    }
}
